package com.example.owner.lettery1.ui


import android.content.Intent
import android.support.v4.app.Fragment
import android.util.Log
import android.view.View
import com.example.owner.lettery1.adapter.BaseRecyclerViewFragment
import com.example.owner.lettery1.adapter.OpenLotteryAdapter
import com.example.owner.lettery1.bean.LotteryBean
import com.example.owner.lettery1.modlus.LotteryPresenter
import com.example.owner.lettery1.modlus.OnItemListener
import java.util.*

class OpenLotteryRoomFragment : BaseRecyclerViewFragment<LotteryPresenter>() {

    private var newList = ArrayList<LotteryBean>()
    override fun onLoadData() {
        showOnLoading()
        val observable = presenter!!.getLottoredBeans()
        observable.subscribe({ data ->
            hideOnLoading()
            setupItemAdapter(data)
        },{t -> Log.e("OpenLotteryRoomFragment","e =========> ${t.message}" )  })
    }

    private fun setupItemAdapter(data: ArrayList<LotteryBean>) {
      for(bean in data){
          if(bean.lotteryName != "胜负彩" && bean.lotteryName!="任九" && bean.lotteryName != "半全场"&& bean.lotteryName != "进球彩"){
              newList.add(bean)
          }
      }
        val adapter = OpenLotteryAdapter(newList)
        recyclerView!!.adapter = adapter
        adapter.onItemListener = object : OnItemListener<LotteryBean> {
           override fun onClick(bean: LotteryBean) {
                val intent = Intent(appActivity,ChampionActivity::class.java)
               intent.putExtra(ChampionActivity.LOTTERY_KEY,bean)
                startActivity(intent)
            }

        }
    }


    override fun onInitView() {
        super.onInitView()
        setFixView(View.VISIBLE)
    }

    override fun onCreatePresenter(): LotteryPresenter = LotteryPresenter()

    companion object {
        fun newInstance() : Fragment = OpenLotteryRoomFragment()
    }

}